package com.pktechan.manage.service.impl;


import com.pktechan.manage.base.BaseMapper;
import com.pktechan.manage.base.BaseServiceImpl;
import com.pktechan.manage.dao.CommissionWithdrawFlowMapper;
import com.pktechan.manage.entity.CommissionBalance;
import com.pktechan.manage.entity.CommissionWithdrawFlow;
import com.pktechan.manage.enumeration.StatusFlowTypeEnum;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.sendMiniMessage.BaseSendMiniMessage;
import com.pktechan.manage.sendMiniMessage.SendMiniMessageStrategy;
import com.pktechan.manage.sendMiniMessage.bean.ReferrerCommissionConfirmMessage;
import com.pktechan.manage.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.manage.service.CommissionBalanceService;
import com.pktechan.manage.service.CommissionWithdrawFlowService;
import com.pktechan.manage.service.SendMessageService;
import com.pktechan.manage.service.StatusFlowService;
import com.pktechan.manage.utils.SystemConstants;
import com.pktechan.manage.vo.CommissionWithdrawFlowVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("commissionWithdrawFlowService")
public class CommissionWithdrawFlowServiceImpl extends BaseServiceImpl<CommissionWithdrawFlow> implements CommissionWithdrawFlowService {

    private static final Logger logger = LoggerFactory.getLogger(CommissionWithdrawFlowServiceImpl.class);

    @Autowired
    private CommissionWithdrawFlowMapper commissionWithdrawFlowMapper;
    @Autowired
    private CommissionBalanceService commissionBalanceService;
    @Autowired
    private StatusFlowService statusFlowService;
    @Autowired
    private SendMessageService sendMessageService;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<CommissionWithdrawFlow> getBaseMapper() {
        return commissionWithdrawFlowMapper;
    }

    /**
     * 查询客户佣金提现管理列表
     *
     * @param commissionWithdrawFlowVo
     * @return
     */
    @Override
    public List<CommissionWithdrawFlowVo> getCommissionWithdrawFlowVoList(CommissionWithdrawFlowVo commissionWithdrawFlowVo) {
        return commissionWithdrawFlowMapper.getCommissionWithdrawFlowVoList(commissionWithdrawFlowVo);
    }

    /**
     * 根据id修改佣金提现状态
     *
     * @param commissionWithdrawFlow
     */
    @Override
    public void updateCommissionWithdrawStatusById(CommissionWithdrawFlow commissionWithdrawFlow, Integer exeCustId) {
        logger.debug("查询id=" + commissionWithdrawFlow.getId() + "的佣金提现信息");
        CommissionWithdrawFlow byId = (CommissionWithdrawFlow) commissionWithdrawFlowMapper.getById(commissionWithdrawFlow.getId());
        if (byId == null) {
            throw new CustomException("未查询到提现信息");
        }
        if (byId.getStatus().equals(SystemConstants.YesOrNo.YES.getICode())) {
            throw new CustomException("已提现完成，请不要在次确认提现");
        }
        logger.debug("查询id=" + byId.getCommissionBalanceId() + "的佣金信息");
        CommissionBalance commissionBalanceById = commissionBalanceService.getById(byId.getCommissionBalanceId());
        //判断是提现确认  还是  提现驳回
        CommissionBalance commissionBalance = new CommissionBalance();
        if (commissionWithdrawFlow.getStatus().equals(SystemConstants.YesOrNo.YES.getICode())) {
            logger.debug("提现确认");
            commissionBalance.setId(byId.getCommissionBalanceId());
            commissionBalance.setWithdrawFreezeAmount(commissionBalanceById.getWithdrawFreezeAmount().subtract(byId.getWithdrawAmount()));//提现冻结-申请提现金额=提现冻结
        } else {
            logger.debug("提现驳回");
            commissionBalance.setId(byId.getCommissionBalanceId());
            commissionBalance.setWithdrawFreezeAmount(commissionBalanceById.getWithdrawFreezeAmount().subtract(byId.getWithdrawAmount()));//提现冻结-申请提现金额=提现冻结
            commissionBalance.setAvailableBalance(commissionBalanceById.getAvailableBalance().add(byId.getWithdrawAmount()));//可用余额+申请提现金额=可用余额
        }
        logger.debug("修改佣金提现冻结份额");
        commissionBalanceService.update(commissionBalance);
        logger.debug("修改佣金提现状态");
        commissionWithdrawFlowMapper.update(commissionWithdrawFlow);
        statusFlowService.statusFlowSave(StatusFlowTypeEnum.COMMISSION_WITHDRAWAL.getCode(), commissionWithdrawFlow.getId(), byId.getStatus(), commissionWithdrawFlow.getStatus(), exeCustId);
        //推荐人接收-厂家-佣金提现确认-提现成功通知
        ReferrerCommissionConfirmMessage referrerCommissionConfirmMessage = sendMessageService.getReferrerCommissionConfirmMessage(commissionWithdrawFlow.getId());
        SendMiniMessageStrategy referrerCommissionConfirmStrategy = BaseSendMiniMessage.getSendMiniMessage(SendMiniMessageEnum.REFERRER_COMMISSION_CONFIRM);
        referrerCommissionConfirmStrategy.sendMiniMessageToActiveMq(referrerCommissionConfirmMessage);
    }
}
